package com.xy.modular.building.action;
import com.xy.modular.building.entity.Room;
import com.xy.modular.building.service.IRoomService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMethod;
import org.apache.shiro.authz.annotation.RequiresRoles;
import javax.servlet.http.HttpServletRequest;

import com.xy.framework.pager.PageRequest;
import com.xy.framework.mreturn.MReturnObject;
import com.xy.framework.mreturn.MReturnUntil;
import com.xy.framework.base.controller.BaseController;

/**
 *
 * Created by BCK on 2019/10/02.
 */
@Controller
@RequestMapping("/room/")
public class RoomController extends BaseController {

	static Logger log = LoggerFactory.getLogger(RoomController.class);
	
    @Autowired
    IRoomService roomService;
    
	@RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public @ResponseBody MReturnObject listModule(Room room, HttpServletRequest request) {
		PageRequest pageRequest = newPageRequest(request);
		return new MReturnObject(MReturnObject.SUCCESS, roomService.findColumnsPageAll(pageRequest),
				roomService.getCount(pageRequest));
	}
    @RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
    public @ResponseBody MReturnObject save(Room room) {
        roomService.save(room);
        return new MReturnObject(MReturnObject.SUCCESS);
    }

    @RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody MReturnObject update(Room room) {
		try {
			roomService.validateEntity(room);
			roomService.update(room);
		} catch (Exception e) {
			log.info("修改失败", e);
			return new MReturnObject(MReturnObject.ERROR, MReturnUntil.ERROR_MOUDLE_SAVE);
		}
		return new MReturnObject(MReturnObject.SUCCESS);
	}

	@RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/del/{id}", method = RequestMethod.GET)
	public @ResponseBody MReturnObject del(@PathVariable String id) {
		try {
			roomService.deleteById(id);
		} catch (Exception e) {
			log.info("删除失败", e);
			return new MReturnObject(MReturnObject.ERROR, MReturnUntil.ERROR_MOUDLE_SAVE);
		}
		return new MReturnObject(MReturnObject.SUCCESS);
	}
}
